﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.Core.Modelling.Primitives;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Modelling.Components
{
    [Serializable]
    public class ShprossHook
    {
        public ModelPoint Position
        {
            get
            {
                return shpross.Primitives[0].GetPoint(this.offset);
            }
        }

        public double Radius
        {
            get
            {
                Interval interval = this.Interval;
                double startLength = interval.Start * shpross.Length;
                double endLength = interval.End * shpross.Length;
                return (endLength - startLength) / 2;                
            }
        }

        public Interval Interval
        {
            get
            {
                double offsetRadius = this.GetOffsetRadius();
                return new Interval(this.offset - offsetRadius / 2, this.offset + offsetRadius / 2); 
            }
        }

        private double offset;

        public double Offset
        {
            get { return offset; }
        }
        private Shpross shpross;

        public ShprossHook(double offset, Shpross shpross)
        {
            this.offset = offset;
            this.shpross = shpross;           
        }

        public bool IsPointInHook(ModelPoint point)
        {
            double offset = this.GetOffsetByPoint(point);
            if (double.IsNaN(offset))
                return false;

            if (this.Interval.IsInInterval(offset))
                return true;
            else
                return false;
        }

        public double GetOffsetByPoint(ModelPoint point)
        {
            double _offset = (shpross.Primitives[0] as IWidePrimitive).GetPathLengthFromProjectionToStart(point);
            _offset = _offset / shpross.Length;
            return _offset;
        }

        public double GetOffsetRadius()
        {
            double _offset = shpross.Width / shpross.Length;
            return _offset;
        }    
    }
}
